package com.chaosj.algorithm

class ValidParentheses {
    static void main(String[] args){
        def str = "[{}][[]]{}[]{}()"
        System.out.println(solution(str))
    }

    static boolean solution(String str){
        if(str!=null && str.length()==0 ){
            return true
        }
        if(str!=null && (str.length()%2!=0)){
            return false
        }
        Map<Character,Character> leftRight = new HashMap<>()
        leftRight.put("(".charAt(0),")".charAt(0))
        leftRight.put("[".charAt(0),"]".charAt(0))
        leftRight.put("{".charAt(0),"}".charAt(0))
        def stack = new Stack()
        for (int i = 0; i < str.length(); i++) {
            Character v = str.charAt(i)
            if( leftRight.values().contains(v) ){
                if( !stack.isEmpty() && leftRight.get(stack.peek())==v ){
                    stack.pop()
                }else{
                    return false
                }
            }else{
                stack.push(v)
            }
        }
        stack.isEmpty()
    }
}
